package com.jz.community.moduleshoppingguide.neighbor.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.neighbor.bean.CirclePersonsBean;
import com.jz.community.moduleshoppingguide.neighbor.presenter.NeighborPersonsPresenter;
import com.jz.community.moduleshoppingguide.neighbor.ui.NeighborPersonsView;
import com.jz.community.moduleshoppingguide.neighbor.ui.adapter.NeighborPersonsAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NeighborPersonsActivity extends BaseMvpActivity<NeighborPersonsView.View, NeighborPersonsPresenter> implements NeighborPersonsView.View, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R2.id.circle_persons_back)
    ImageView circlePersonsBack;
    @BindView(R2.id.circle_persons_title)
    TextView circlePersonsTitle;
    @BindView(R2.id.circle_persons_toolbar)
    Toolbar circlePersonsToolbar;
    @BindView(R2.id.circle_persons_rv)
    RecyclerView circlePersonsRv;
    @BindView(R2.id.circle_persons_refresh)
    SmartRefreshLayout circlePersonsRefresh;


    private String circle_id="";
    private String circle_name;
    private String circle_persons;

    private View noDataView;//无数据显示
    private View loadingView;//加载显示
    private View errorView;//网络错误显示

    private NeighborPersonsAdapter adapter_persons;
    private List<CirclePersonsBean.EmbeddedBean.ContentBean> circle_list = new ArrayList<>();

    private int page = 0;
    private int totalPage = 0;
    private int size = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_shoppingguide_activity_neighbor_persons;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void initView() {
        super.initView();
        ImmersionBar.with(this)
                .titleBar(circlePersonsToolbar)
                .statusBarDarkFont(true, 0.2f)//当白色背景状态栏遇到不能改变状态栏字体为深色的设备时，解决方案
                .init();

        //社区ID
        circle_id = getIntent().getStringExtra("circle_id");
        //社区名称
        circle_name = getIntent().getStringExtra("circle_name");
        //社区人数
        circle_persons = getIntent().getStringExtra("circle_persons");

        circlePersonsTitle.setText(circle_name + "(" + circle_persons + ")");

        noDataView = getLayoutInflater().inflate(R.layout.view_empty, (ViewGroup) circlePersonsRv.getParent(), false);

        ImageView empty_iv = (ImageView) noDataView.findViewById(R.id.empty_img);
        TextView empty_tv = (TextView) noDataView.findViewById(R.id.empty_tv);

        empty_iv.setImageResource(R.mipmap.empty_default);
        empty_tv.setText("该圈子暂无用户！");

        errorView = getLayoutInflater().inflate(R.layout.view_error, (ViewGroup) circlePersonsRv.getParent(), false);

        ImageView error_iv = (ImageView) errorView.findViewById(R.id.error_img);
        error_iv.setImageResource(R.mipmap.error_default);

        loadingView = getLayoutInflater().inflate(R.layout.view_loading, (ViewGroup) circlePersonsRv.getParent(), false);

        ImageView loading_iv = (ImageView) loadingView.findViewById(R.id.loading_img);

        GlideApp.with(mActivity)
                .asGif()
                .load(R.drawable.loading_default)
                .into(loading_iv);

        errorView.setOnClickListener(v -> {
            adapter_persons.setEmptyView(loadingView);
            mPresenter.initPersons(circle_id, page, size);
        });

        mPresenter.initPersons(circle_id, page, size);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        initRecyclerView();

    }

    private void initRecyclerView() {
        GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 5);
        circlePersonsRv.setHasFixedSize(true);
        circlePersonsRv.setFocusable(false);
        circlePersonsRefresh.setOnRefreshListener(this);//刷新监听
        circlePersonsRefresh.setEnableLoadMore(false);
        circlePersonsRv.setLayoutManager(layoutManager);

        adapter_persons = new NeighborPersonsAdapter(R.layout.module_shoppingguide_item_circle_persons, circle_list);
        adapter_persons.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        adapter_persons.setOnLoadMoreListener(this, circlePersonsRv);
        circlePersonsRv.setAdapter(adapter_persons);

        adapter_persons.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mActivity, NeighborPersonInfoActivity.class);
            intent.putExtra("user_id", adapter_persons.getData().get(position).getUserId());
            startActivity(intent);
        });

    }

    @Override
    protected NeighborPersonsPresenter createPresenter() {
        return new NeighborPersonsPresenter(this);
    }

    @Override
    public void setPersonsData(CirclePersonsBean entity) {
        freshEnd();
        //总页数
        totalPage = entity.getPage().getTotalPages();

        if (totalPage <= 0) {
            //先设置空布局
            if (page == 0) {
                adapter_persons.replaceData(new ArrayList<>());
                adapter_persons.setEmptyView(noDataView);
                adapter_persons.notifyDataSetChanged();
            } else {
                adapter_persons.setEmptyView(noDataView);
                adapter_persons.notifyDataSetChanged();
            }
        } else {
            circle_list = entity.get_embedded().getContent();
            if (page == 0) {
                adapter_persons.setNewData(circle_list);
                //如果数据少 不显示已加载全部
                adapter_persons.disableLoadMoreIfNotFullPage();
            } else {
                adapter_persons.addData(circle_list);

            }
        }

        adapter_persons.loadMoreComplete();


    }

    @Override
    public void showError(String s) {
        freshEnd();
        if (null != adapter_persons) {
            adapter_persons.setEmptyView(errorView);
        }

    }

    private void freshEnd() {
        circlePersonsRefresh.finishRefresh(200);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        if (null != adapter_persons) {
            adapter_persons.setEnableLoadMore(false);
            adapter_persons.replaceData(new ArrayList<>());
            adapter_persons.setEmptyView(loadingView);
        }
        page = 0;
        mPresenter.initPersons(circle_id, page, size);
        adapter_persons.setEnableLoadMore(true);

    }

    @Override
    public void onLoadMoreRequested() {
        circlePersonsRefresh.setEnableRefresh(false);
        if (totalPage > 0) {
            if (page < totalPage - 1) {
                page++;
                mPresenter.initPersons(circle_id, page, size);
                circlePersonsRefresh.setEnableRefresh(true);
            } else {
                adapter_persons.loadMoreEnd();
                circlePersonsRefresh.setEnableRefresh(true);
            }
        }

    }

    @OnClick(R2.id.circle_persons_back)
    public void backClick() {
        finish();
    }


}
